package pers.cz.netty.protocal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: 定义消息协议
 * @author: Cheng Zhi
 * @create: 2022-10-04 15:21
 **/
public class Message {

    /**
     * 具体传输的数据
     */
    private Object content;

    /**
     * 分割标识
     */
    private String splitSymbol = "$POSTGIRL$";

    /**
     * 数据包总长度
     */
    private long contentLength;

    /**
     * 消息类型
     */
    private MsgType msgType;

    /**
     * 消息附件
     */
    private Map<String, String> attachments = new HashMap<>();

    private String ip;

    private int port;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    /**
     * 获取附件信息，但是不可修改
     * @return
     */
    public Map<String, String> getAttachments() {
        return Collections.unmodifiableMap(attachments);
    }

    /**
     * 设置附件，该方法会清空所有原先得附件信息
     * @param attachments
     */
    public void setAttachments(Map<String, String> attachments) {
        this.attachments.clear();
        if (null != attachments) {
            this.attachments.putAll(attachments);
        }
    }

    /**
     * 添加附件
     * @param key
     * @param value
     */
    public void addAttachment(String key, String value) {
        attachments.put(key, value);
    }

    /**
     * 获取一个附件
     * @param key
     * @return
     */
    public String getAttachment(String key) {
        return attachments.get(key);
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public long getContentLength() {
        return contentLength;
    }

    public void setContentLength(long contentLength) {
        this.contentLength = contentLength;
    }

    public MsgType getMsgType() {
        return msgType;
    }

    public void setMsgType(MsgType msgType) {
        this.msgType = msgType;
    }

    public String getSplitSymbol() {
        return splitSymbol;
    }
}
